/**
 * Witontek.com.
 * Copyright (c) 2012-2017 All Rights Reserved.
 */
package com.witon.witontool.web.controller.dbcheck;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.witon.witontool.core.model.db.DbConfigRepository;
import com.witon.witontool.core.model.db.model.DbConfig;
import com.witon.witontool.core.service.DbCheckService;
import com.witon.witontool.core.service.DbCodeService;
import com.witon.witontool.web.controller.dbcheck.model.CheckResult;
import com.witon.witontool.web.controller.dbcheck.model.DbCodeResult;
import com.witon.witontool.web.controller.dbcheck.model.Table;

/**
 * 
 * @author Long
 * @version $Id: IndexController.java, v 0.1 2017年6月30日 上午8:54:22  xiaolong.ma  Exp $
 */
@Controller
@RequestMapping({ "/dbcheck" })
public class DbCheckController {
    private final Logger       logger   = LoggerFactory.getLogger(this.getClass());

    private String             listView = "/dbcheck/list";
    private String             editView = "/dbcheck/edit";

    private String             infoView = "/dbcheck/info";

    private String             codeView = "/dbcheck/code";

    @Autowired
    private DbConfigRepository dbConfigRepository;

    @Autowired
    private DbCheckService     dbCheckService;

    @Autowired
    private DbCodeService      dbCodeService;

    @RequestMapping({ "/list" })
    public String test(HttpServletRequest request, ModelMap map) {
        logger.info("test...");
        List<DbConfig> list = dbConfigRepository.queryList();
        map.put("list", list);
        return listView;
    }

    @RequestMapping({ "/delete" })
    public String delete(HttpServletRequest request, int id) {
        logger.info("delete id={}", id);
        dbConfigRepository.deleteById(id);

        return "redirect:" + listView;
    }

    @RequestMapping({ "/edit" })
    public String edit(HttpServletRequest request, ModelMap map, int id) {
        logger.info("test...");
        DbConfig m = dbConfigRepository.getById(id);
        map.put("m", m);
        map.put("action", "edit");
        return editView;
    }

    @RequestMapping(value = "/editSubmit")
    public String editSubmit(Map<String, Object> map, HttpServletRequest request, DbConfig model,
                             String action) {

        if (action.equals("add")) {
            dbConfigRepository.add(model);
        } else {
            dbConfigRepository.update(model);
        }

        return "redirect:" + listView;

    }

    @RequestMapping({ "/add" })
    public String add(HttpServletRequest request, ModelMap map) {
        logger.info("test...");
        DbConfig m = new DbConfig();

        map.put("m", m);
        map.put("action", "add");
        return "/dbcheck/edit";
    }

    @RequestMapping({ "/check" })
    public String check(HttpServletRequest request, ModelMap map, int id) {
        logger.info("test...");
        CheckResult r = dbCheckService.dbCheck(id);
        map.put("result", r);
        return infoView;
    }

    @RequestMapping({ "/tableList" })
    public String tableList(HttpServletRequest request, ModelMap map, int id) {
        logger.info("tableList...");
        List<Table> list = dbCodeService.getTableList(id);
        map.put("list", list);
        map.put("id", id);
        return codeView;
    }

    @RequestMapping({ "/code" })
    public String code(HttpServletRequest request, ModelMap map, int id, String tableName) {
        logger.info("tableList...");
        List<Table> list = dbCodeService.getTableList(id);
        map.put("list", list);

        DbCodeResult r = dbCodeService.dbCode(list, tableName);
        map.put("r", r);
        map.put("id", id);
        return codeView;
    }
}
